package com.jar.exercise.多线程.AQS.Condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Main2 {
    public static void main(String[] args) throws InterruptedException {
        // 创建锁对象
        ReentrantLock lock = new ReentrantLock();
        // 创建条件变量
        Condition condition = lock.newCondition();
        // 以下创建两个线程，里面都会获取锁和释放锁
        Thread thread1 = new Thread(() -> {
            lock.lock();
            try {
                System.out.println("await begin");
                // 注意，这里调用条件变量的await方法，当前线程就会丢到condition条件变量中的条件队列中阻塞
                condition.await();
                System.out.println("await end");
            } catch (InterruptedException e) {
                //
            } finally {
                lock.unlock();
            }

        });

        Thread thread2 = new Thread(() -> {
            lock.lock();
            try {
                System.out.println("signal begin");
                // 唤醒被condition变量内部队列中的某个线程
                condition.signal();
                System.out.println("signal end");
            } finally {
                lock.unlock();
            }
        });
        thread1.start();
        Thread.sleep(500);
        thread2.start();
    }
}
